<template>
	<view class="content">
		<view class="content-item" style="margin-top: 0;">
			投资钱包余额：{{userInfo.touzi}} 元
		</view>
		<view class="content-item">
			投资金额
			<uni-data-checkbox mode="tag" v-model="form.amountId" :localdata="amountList"></uni-data-checkbox>
		</view>
		
		<view class="content-item">
			<input type="password" value="" v-model="form.pay_pass" placeholder="请输入交易密码" style="border: 1px solid #2979ff;border-radius: 10px;padding: .4rem;text-align: center;"/>
		</view>
		<view class="content-item">
			<button style="border-radius: 10px;color: white;text-align: center;background: #dd5154;" @click="doTouzi()">提交</button>
		</view>
	</view>
	<uni-section title="投资记录" type="line" style="background: #f4f4f4;"></uni-section>
	<view class="content-two">
		<view class="content-two-item" v-for="item,index in logList" :key="index">
			<view>
				<text>投资钱包：</text>
				<text>投资钱包</text>
			</view>
			<view>
				<text>类型：</text>
				<text>新手投资</text>
			</view>
			<view>
				<text>金额：</text>
				<text>{{item.amount}}</text>
			</view>
			<view>
				<text>日收益比例：</text>
				<text>{{item.fee}}%</text>
			</view>
			<view>
				<text>封顶倍数：</text>
				<text>{{item.feng}}</text>
			</view>
			<view>
				<text>投入时间：</text>
				<text>{{item.date}}</text>
			</view>
		</view>
	</view>
	
	
</template>

<script>
	export default {
		data() {
			return {
				amount: '0.00',
				amountList:[
					{
						text:'500.00',
						value:1
					},
					{
						text:'1000.00',
						value:2
					},
					{
						text:'1500.00',
						value:3
					},
					{
						text:'2000.00',
						value:4
					},
					{
						text:'3000.00',
						value:5
					},
				],
				form:{
					amountId:null,
					pay_pass:null,
				},
				logList:null,
				userInfo:null,
				user_token:null
			};
		},
		onLoad() {
			this.getUserInfo()
			this.getTouziList()
			this.getOrderList()
		},
		methods:{
			getUserInfo(){
				let that = this
				if(that.userInfo == null){
					uni.getStorage({
						key: 'user_token',
						success: function (res) {
							that.user_token = res.data
							that.checkUser()
						},
						fail: function(res){
							uni.showToast({
								title:'请先登录',
								icon:'none',
								success() {
									uni.redirectTo({
										url:'/pages/login/login',
									})
								}
							})
							
						}
					});
				}
			},
			checkUser(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getUserInfo',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						if(res.data.code == 401){
							uni.removeStorage({
								key:'user_token',
								success() {
									uni.showToast({
										icon:'none',
										title:'登录已失效,请重新登录',
										success() {
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/login/login'
												})
											})
										}
									})
								}
							})
						}
						that.userInfo = res.data.data
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			getOrderList(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/touzi/index',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						that.logList = res.data.data
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
				
			},
			getTouziList(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/touzi/getTouzi',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						that.amountList = res.data.data
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
				
			},
			doTouzi(){
				let that = this
				if(!that.form.amountId){
					uni.showToast({
						title:'未选择投资金额'
					})
					return false;
				}
				uni.request({
					url:getApp().globalData.baseUrl + '/api/touzi/doTouzi',
					method:'POST',
					header: {token:that.user_token},
					data: that.form,
					success(res) {
						if(res.data.code == 1){
							uni.showToast({
								title:res.data.msg,
								icon:'none',
								success() {
									setTimeout(function(){
										window.location.reload()
									},1000)
								}
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none',
							})
						}
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f4f4f4;
	}
	.content{
		display: flex;
		flex-direction: column;
		background: white;
		margin-top: .4rem;
		padding: 1rem;
		font-size: 16px;
		font-weight: bold;
	}
	.content-item{
		margin-top: 1rem;
	}
	.uni-data-checklist .checklist-group .checklist-box.is--tag{
		width: 24%;
	}
	uni-data-checkbox{
		margin-top: 1rem;
	}
	.content-two{
		display: flex;
		flex-direction: column;
		margin-top: .4rem;
	}
	.content-two-item{
		margin-top: .2rem;
		display: flex;
		flex-wrap: wrap;
		padding: .7rem;
		background:white;
		
	}
	.content-two-item>view{
		width: 47%;
		font-size: 12px;
	}
</style>
